package com.whnk.attendance.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.whnk.attendance.vo.SocialInsuranceInfoQueryVo;
import com.whnk.attendance.vo.SocialInsuranceInfoListVo;
import com.whnk.attendance.vo.SocialInsuranceInfoUpdateVo;
import com.whnk.attendance.vo.SocialInsuranceListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import com.whnk.attendance.service.ISocialInsuranceInfoService;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 社保列表Controller
 *
 * @author whnk
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/socialInsurance")
@Api(tags = "社保列表")
public class SocialInsuranceInfoController extends BaseController
{
    @Autowired
    private ISocialInsuranceInfoService socialInsuranceInfoService;

    /*
     * 分页查询社保列表
     */
    @PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询社保列表", notes = "分页查询社保列表")
    public TableDataInfo list(@ApiParam(value = "社保信息", required = false) SocialInsuranceInfoQueryVo socialInsuranceInfoQueryVo)
    {
        startPage();
        List<SocialInsuranceInfoListVo> list = socialInsuranceInfoService.selectSocialInsuranceInfoListVoList(socialInsuranceInfoQueryVo);
        return getDataTable(list);
    }

    /*
     * 根据逻辑主键查询社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping(value = "/query/{socialInsId}")
    @ApiOperation(value = "根据逻辑主键查询社保信息", notes = "根据逻辑主键查询社保信息")
    public AjaxResult getInfo(@ApiParam(value = "逻辑主键", required = false) @PathVariable("socialInsId") Long socialInsId)
    {
        return AjaxResult.success(socialInsuranceInfoService.selectSocialInsuranceInfoBySocialInsId(socialInsId));
    }

    /**
     * 新增社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "社保信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增社保信息", notes = "新增社保信息")
    public AjaxResult add(@ApiParam(value = "社保信息", required = false) @Validated @RequestBody SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo)
    {
        if(!socialInsuranceInfoService.checkSocialInsNum(socialInsuranceInfoUpdateVo)){
            return AjaxResult.error("新增社保信息" + socialInsuranceInfoUpdateVo.getSocialInsNum() + "失败，该社保信息已存在");
        }
        return toAjax(socialInsuranceInfoService.insertSocialInsuranceInfo(socialInsuranceInfoUpdateVo));
    }

    /**
     * 修改社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "社保信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改社保信息", notes = "修改社保信息")
    public AjaxResult edit(@ApiParam(value = "社保信息", required = false) @Validated @RequestBody SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo)
    {
        if(!socialInsuranceInfoService.checkSocialInsNum(socialInsuranceInfoUpdateVo)){
            return AjaxResult.error("修改社保信息" + socialInsuranceInfoUpdateVo.getSocialInsNum() + "失败，该社保信息已存在");
        }
        return toAjax(socialInsuranceInfoService.updateSocialInsuranceInfo(socialInsuranceInfoUpdateVo));
    }

    /**
     * 批量删除社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "社保信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{socialInsIds}")
    @ApiOperation(value = "批量删除社保信息", notes = "批量删除社保信息")
    public AjaxResult remove(@ApiParam(value = "逻辑主键", required = false) @PathVariable Long[] socialInsIds)
    {
        return toAjax(socialInsuranceInfoService.deleteSocialInsuranceInfoBySocialInsIds(socialInsIds));
    }

    /**
     * 导出社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "社保信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出社保信息", notes = "导出社保信息")
    public void export(HttpServletResponse response, @ApiParam(value = "社保信息", required = false) SocialInsuranceInfoQueryVo socialInsuranceInfoQueryVo) throws IOException
    {
        List<SocialInsuranceInfoListVo> list = socialInsuranceInfoService.selectSocialInsuranceInfoListVoList(socialInsuranceInfoQueryVo);
        ExcelUtil<SocialInsuranceInfoListVo> util = new ExcelUtil<SocialInsuranceInfoListVo>(SocialInsuranceInfoListVo.class);
        util.exportExcel(response, list, "社保信息数据");
    }

    /**
     * 导入社保信息
     */
    @PreAuthorize(hasPermi = "attendance:info:import")
    @Log(title = "社保信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入社保信息", notes = "导入社保信息")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SocialInsuranceInfoListVo> util = new ExcelUtil<>(SocialInsuranceInfoListVo.class);
        List<SocialInsuranceInfoListVo> socialInsuranceInfoList = util.importExcel(file.getInputStream());
        String message = socialInsuranceInfoService.importData(socialInsuranceInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 逻辑主键id，社保分类编号name接口
     */
    @GetMapping("/socialInsuranceList")
    @ApiOperation(value = "逻辑主键id，社保分类编号name接口",notes = "逻辑主键id，社保分类编号name接口")
    public TableDataInfo socialInsuranceList(){
        List<SocialInsuranceListVo> list = socialInsuranceInfoService.socialInsuranceList();
        return getDataTable(list);
    }
}
